package cn.xs.user.action;

import java.util.UUID;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;

import cn.xs.core.util.TimeUtils;
import cn.xs.user.entity.Sign;
import cn.xs.user.entity.User;
import cn.xs.user.service.UserService;

import com.opensymphony.xwork2.ActionSupport;


/**
 * 负责用户模块的控制器
 *     包括:登录,注册,购物车,我的信息...
 * @author xs
 *
 */
public class UserAction extends ActionSupport {

	@Resource
	private UserService userService;
	
	private User user;
	
	private String pwd;		//旧密码
	private String rpwd;	//新密码
	
	/**
	 * 跳转到登录页面
	 * @return
	 */
	public String toLoginUI(){
		return "loginUI";
	}
	
	/**
	 * 登录方法,成功后存放用户信息到session中并且跳转到首页去
	 */
	public String login(){
		if(user!=null){
			User userInfo = userService.login(user.getAccount(),user.getPassword());
			if(userInfo !=null ){
				ServletActionContext.getRequest().getSession().setAttribute("USER_INFO", userInfo);
				return "homepage";
			}
		}
		ServletActionContext.getRequest().setAttribute("msg", "登录失败");
		return "loginUI";
	}
	/**
	 * 跳转到注册页面
	 * @return
	 */
	public String toRegisterUI(){
		return "registerUI";
	}
	
	public String toRotateDiscUI(){
		return "rotateDiscUI";
	}
	
	/**
	 * 跳转到个人信息
	 */
	public String toInfoUI(){
		//拿到当前用户
		user = (User) ServletActionContext.getRequest().getSession().getAttribute("USER_INFO"); 
		return "infoUI";
	}
	/**
	 * 注册方法
	 * 		保存数据进行数据库,跳转到登录页面
	 * @return
	 */
	public String register(){
		if(user!=null){
			user.setId(UUID.randomUUID().toString());
			userService.save(user);
			
		}
		return "loginUI";
	}

	/**
	 * 保存修改的密码
	 * @return
	 */
	public String modifyPwd(){
		if(user!=null){
			user = (User) ServletActionContext.getRequest().getSession().getAttribute("USER_INFO");
			//如果旧密码校验通过,就改密码
			if(pwd.equals(user.getPassword())){
				user.setPassword(rpwd);
				userService.update(user);
			}else{
				ServletActionContext.getRequest().setAttribute("msg", "密码不正确");
				return "infoUI";
			}
		}
		return "homepage";
	}
	/**
	 * 退出方法
	 * @return
	 */
	public String quit(){
		ServletActionContext.getRequest().getSession().invalidate();
		return "homepage";
	}
	
	/**
	 * 跳转到签到页面
	 * @return
	 */
	public String toSignUI(){
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("USER_INFO");
		Sign sign = userService.checkSign(user.getId(), new TimeUtils().getTime());
		int count = userService.getSignCountByMonth(user.getId());
		ServletActionContext.getRequest().setAttribute("count", count);
		ServletActionContext.getRequest().setAttribute("sign", sign);
		return "signUI";
	}
	
	/**
	 * 签到
	 * @return
	 */
	public String sign(){
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("USER_INFO");
		userService.sign(user.getId());
		return toSignUI();
	}
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getRpwd() {
		return rpwd;
	}
	public void setRpwd(String rpwd) {
		this.rpwd = rpwd;
	}
	
}
